package hrm.action.insa.militaryaction;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.insa.EmployeeDTO;
import hrm.dto.insa.MilitaryDTO;
import hrm.dto.login.LoginDTO;
import hrm.serviceimple.EmployeeServiceImple;
import hrm.serviceimple.MilitaryServiceImple;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsaListMilitaryAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		EmployeeServiceImple employeeService = EmployeeServiceImple.getInstance();
		EmployeeDTO employeeDTO = new EmployeeDTO();
		employeeDTO.setId(getFindId(request));
		employeeDTO = employeeService.getEmployee(employeeDTO);
			
		MilitaryServiceImple militaryService = MilitaryServiceImple.getInstance();
		MilitaryDTO militaryDTO = new MilitaryDTO();
		militaryDTO.setId(employeeDTO.getId());
		List<MilitaryDTO> militaryList = militaryService.getAllMilitary(militaryDTO);	

		request.setAttribute("employeeDTO", employeeDTO);
		request.setAttribute("militaryList", militaryList);
		actfoward.setUrl("listMilitary");

		return actfoward;
	}
	
	private String getFindId(HttpServletRequest request) {
		String findid = (String)request.getSession().getAttribute("findid");
		if(findid == null){
			findid = ((LoginDTO)request.getSession().getAttribute("login")).getId();
			request.setAttribute("findid", findid);
		}
		return findid;
	}
}
